package com.beemans.common.app.ui.fragments

import android.util.Log
import android.view.View
import androidx.core.os.bundleOf
import com.beemans.common.app.R
import com.beemans.common.app.databinding.FragmentMainBinding
import com.beemans.common.app.ui.adapters.MainAdapter
import com.beemans.common.app.ui.base.BaseFragment
import com.beemans.common.ext.init
import com.beemans.common.ext.jumpFragment
import com.beemans.common.ext.pt2dp
import com.beemans.common.ext.setNbOnItemClickListener
import com.beemans.common.ui.views.CustomDivider
import com.beemans.common.utils.PermissionHelper
import com.blankj.utilcode.util.AppUtils
import com.hjq.permissions.Permission
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig

/**
 * @author tiamosu
 * @date 2021/4/26.
 */
class MainFragment : BaseFragment() {
    private val dataBinding: FragmentMainBinding? by lazyDataBindingOrNull { }
    private val adapter by lazy { MainAdapter() }

    private val features by lazy {
        mapOf(
            "Glide" to R.id.glideFragment,
            "自定义View" to R.id.customViewFragment,
            "倒计时" to R.id.countdownFragment,
            "Http请求" to R.id.httpFragment,
            "LoadSir多状态布局" to R.id.loadSirFragment,
            "ViewPage2" to R.id.vp2Fragment,
            "权限请求" to PERMISSION_CODE,
            "位置信息" to R.id.locationFragment,
            "Sticky吸顶" to R.id.stickyFragment,
            "Exposure" to R.id.exposureFragment,
        )
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_main)
    }

    override fun initView(rootView: View?) {
        dataBinding?.mainRv?.init(
            bindAdapter = adapter,
            itemDecoration = CustomDivider(10.pt2dp.toFloat())
        )
    }

    override fun initEvent() {
        adapter.setNbOnItemClickListener { _, _, position ->
            val key = adapter.data[position]
            val resId = features[key] ?: return@setNbOnItemClickListener

            when (resId) {
                PERMISSION_CODE -> requestPermission()
                else -> jumpFragment(resId, args = bundleOf(PARAM_TITLE to key))
            }
        }
    }

    private fun requestPermission() {
        PermissionHelper.with(this)
            .permission(
                Permission.READ_PHONE_STATE,
                Permission.MANAGE_EXTERNAL_STORAGE
            )
            .onForwardToSettings { true }
            .request { grantedList, deniedList, isGrantedAll, isDeniedNever ->
                if (isGrantedAll) {
                    showToast("权限已授权")
                }
                Log.e(
                    "susu",
                    "onResult   \ngrantedList:$grantedList   \ndeniedList:$deniedList   \nisGrantedAll:$isGrantedAll   \nisDeniedNever:$isDeniedNever"
                )
            }
    }

    override fun onFlyLazyInitView() {
        super.onFlyLazyInitView()
        adapter.setList(features.keys.toMutableList())
    }

    override fun doBusiness() {
    }

    override fun isCheckNetChanged() = true

    override fun refreshNetRequest() {
        super.refreshNetRequest()
        Log.e("susu", "refreshNetRequest")
    }

    override fun onBackPressedSupport(): Boolean {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            AppUtils.exitApp()
        } else {
            TOUCH_TIME = System.currentTimeMillis()
            showToast("再按一次退出")
        }
        return true
    }

    companion object {
        const val PARAM_TITLE = "PARAM_TITLE"

        // 再点一次退出程序时间设置
        private const val WAIT_TIME = 2000L
        private var TOUCH_TIME = 0L

        private const val PERMISSION_CODE = 999
    }
}